<!-- 执行日志 朝合公区电表 | 商户电表 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<div>
				<el-input v-model="ruleName" class="zs-select-md zs-space-right-sm" disabled placeholder="电表名称" clearable />
				<el-select v-model="searchObj.executeResult" filterable clearable class="zs-select-md zs-space-right-sm" placeholder="执行结果" @change="queryList">
					<el-option label="成功" value="1" />
					<el-option label="失败" value="0" />
				</el-select>
				<el-button class="zs-btn-plain" @click="queryList"> 查询 </el-button>
			</div>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" :align="'center'" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="executeCode" label="执行记录id" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="formatExecuteTime" label="实际执行时间" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="executeResult" label="执行结果" show-overflow-tooltip align="center" :formatter="defaultTableData">
					<template #default="{ row }">
						<span>{{ row.executeResult == 1 ? '成功' : '失败' }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="totalNum" label="总表数" show-overflow-tooltip align="right" :formatter="defaultTableData" />
				<el-table-column label="匹配失败数" align="right">
					<template #default="{ row }">
						<el-link :disabled="row.failNum > 0 ? false : true" type="primary" @click="openDrawer(false, 'edit', row, 'fail')">{{ row.failNum }}</el-link>
					</template>
				</el-table-column>
				<el-table-column label="查看" width="90" :align="'center'">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'info')">详情</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>

	<!-- 详情 -->
	<DrawerLogInfo :isShow="drawer.info.isShowDrawer" :isReadOnly="drawer.info.isReadOnly" :handleType="drawer.info.handleType" :currentRow="currentRow" @close="closeDrawer($event, 'info')"> </DrawerLogInfo>

	<!-- 失败数量 -->
	<DrawerLogFail :isShow="drawer.fail.isShowDrawer" :isReadOnly="drawer.fail.isReadOnly" :handleType="drawer.fail.handleType" :currentRow="currentRow" @close="closeDrawer($event, 'fail')"> </DrawerLogFail>
</template>

<script setup>
	import DrawerLogInfo from '../components/drawerLogInfo.vue';
	import DrawerLogFail from '../components/drawerLogFail.vue';
	import { defaultTableData, parseTime } from '@/utils/publicUtils';
	import Energy from '@/api/Energy';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';

	const route = useRoute();
	let store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);
	const ruleName = decodeURI(route.query.ruleName);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let searchObj = reactive({ pageNumber: 1, pageSize: curPageSize.value });

	// 抽屉相关
	let currentRow = ref({failDeviceNumTxt:""});
	let drawer = reactive({
		info: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		fail: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.value = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		data.isRefresh && queryList('init');
	};

	// //列表数据源
	const queryList = async () => {
		tableData.loading = true;
		let params = {
			energyMeterConfigId: route.query.id,
			...searchObj,
		};
		let res = await Energy.doLogListApi(params);
		if (res.data.code !== 200) return;
		res.data.data.list.forEach(item => {
			item.formatExecuteTime = item.executeTime ? parseTime(item.executeTime) : '-';
		});
		tableData.data = res.data.data.list;
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			searchObj.pageSize = size;
		} else {
			searchObj.pageNumber = size;
		}
		queryList();
	};

	queryList();
</script>
